package eu.webhib.extgui.controller.tree.data;

import java.util.List;

import eu.webhib.extgui.data.intf.DataItem;

/**
 * A tree item holding a simple label in a tree component, e.g. to represent the parent node for sub items
 * @param T type of children
 */
public class TreeLabelItem<T extends DataItem> implements DataItem {

   /** The parent of this data item if any */
   private DataItem parent;
   /** All the children this label item holds */
   private List<T> children;
   /** The label string shown in the view */
   private String label;

   /**
    * Creates an root label item
    * @param label label string shown for this node in the view
    */
   public TreeLabelItem(String label) {
      this.parent = null;
      this.label = label;
   }

   /**
    * Creates an label item under the given parent
    * @param parent the parent of this label item
    * @param label label string shown for this node in the view
    */
   public TreeLabelItem(DataItem parent, String label) {
      this.parent = parent;
      this.label = label;
   }

   /*
    * The parent of this tree item
    */
   public DataItem getParent() {
      return parent;
   }

   /*
    * The display label for this item
    * @see DataItem
    */
   @Override
   public String getLabel() {
      return label;
   }

   /**
    * The children of this item
    */
   public List<T> getChildren() {
      return children;
   }

   /**
    * Set the children of this item
    */
   public void setChildren(List<T> children) {
      this.children = children;
   }

   /**
    * Whether this item is identical (equal) to the given tree item
    * @param other Other item to compare
    * @return whether this item is identical to the other DataItem
    * @see DataItem
    */
   @Override
   public boolean identical(DataItem obj) {
      assert (obj != null);
      if (this.getClass() != obj.getClass()) {
         return false;
      }
      /** Type is checked right before */
      @SuppressWarnings("unchecked")
      TreeLabelItem<T> other = (TreeLabelItem<T>) obj;
      if (this.label.equals(other.label)
              && this.parent == other.parent) {
         return true;
      }
      return (this.parent != null && other.parent != null)
              && this.parent.identical(other.parent);
   }

   /**
    * Only used for debug purpose
    */
   @Override
   public String toString() {
      return label;
   }
}
